/**
  * Simple OpenVG example which renders a bitmap background and a string of spheres following a
  * sine curve.
  *
  * This code runs about 25 fps on Nokia 500 device, 60 fps on Nokia E7 device.
  *
  */

#include <QEvent>
#include "game.h"
#include "droidworld.h"
#include <qDebug>

// for readability, the screen and texture sizes are hard-coded
const float BACKGROUND_WIDTH=1024;
const float BALL_WIDTH=128.0f;
const float BALL_HEIGHT=128.0f;
const float SCREEN_WIDTH=640.0f;
const float SCREEN_HEIGHT=360.0f;

const float BACKGROUND_SCROLL_SPEED=50.0f;
const float BALL_PHASE=0.0111f;
const float BALL_SCALE=120.0f;

// image resources
VGImage bg;

// Droid world
DroidWorld *world;

// Player object
DroidObject *playerObject;


/** walltime */
float t;


void game_init()
{
    // Create the world.
    world = new DroidWorld(SCREEN_WIDTH, SCREEN_HEIGHT );

    // Add the player object
    playerObject = world->addObject( new DroidObject(world, ePLAYER ) );


    // add some random asteroids
    for (int f=0; f<8; f++)
        world->addObject( new DroidObject( world, eASTEROID ));


    // add a planet
    world->addObject( new DroidObject( world, ePLANET ));

}

void game_destroy()
{
    delete world;
}

void game_prepare()
{
    vgSeti(VG_RENDERING_QUALITY, VG_RENDERING_QUALITY_BETTER);
    vgSeti(VG_IMAGE_QUALITY, VG_IMAGE_QUALITY_BETTER);

    world->prepeare();
    bg = loadVGImage("images/orange_bg_mountain.png");
}

void game_release()
{
    world->release();
    vgDestroyImage(bg); bg=VG_INVALID_HANDLE;
}

void game_update(float walltime)
{
    t=walltime;
    static float extime = walltime;
    float deltaTime = walltime - extime;
    extime = walltime;

    if (deltaTime>0.25f) deltaTime = 0.25f;
    world->run( deltaTime );
}

void game_render()
{
    // clear background using black
    VGfloat color[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
    vgSetfv( VG_CLEAR_COLOR, 4, color );
    vgClear( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);


    // render scrolling background bitmap (twice for looping effect)
    vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
    vgLoadIdentity();
    vgTranslate(-fmod(t*BACKGROUND_SCROLL_SPEED,BACKGROUND_WIDTH), 0);
    vgDrawImage(bg);
    vgTranslate(BACKGROUND_WIDTH, 0);
    vgDrawImage(bg);

    world->draw();

    /*
    // render sine spheres
    for(int i=-BALL_WIDTH/2;i<SCREEN_WIDTH+BALL_WIDTH/2;i+=BALL_WIDTH/4)
    {
        vgLoadIdentity();
        vgTranslate(i, (SCREEN_HEIGHT-BALL_HEIGHT)/2+sin(t+i*BALL_PHASE)*BALL_SCALE);
        vgDrawImage(ball);
    }
    */
}

void game_event(int type, float x, float y, float z)
{
    switch (type) {
    case Qt::TouchPointPressed:
        world->addObject( new DroidObject( world, eASTEROID ))->getPosition() =
                QVector3D( (x/SCREEN_WIDTH-0.5f), -(y/SCREEN_HEIGHT - 0.5f), 0);
        break;

        /*
    Qt::TouchPointMoved	0x02	The touch point moved.
    Qt::TouchPointStationary	0x04	The touch point did not move.
    Qt::TouchPointReleased
    */
    }
}
